#class to load a lesson plan from a file
class Lessons:


    def __init__(self):
        self.allLessons = []
        self.lessonWords = {}
        self.numLessons = 0
        self.lessonLengths = {}

    #returns all the "words" in a particular lesson 
    #returns an empty dictionary if there is no such lesson for key lessonName.
    def getLessonWords(self, lessonName):
        if self.lessonWords.__contains__(lessonName):
            return self.lessonWords[lessonName]
        return {}

    #returns the list of all lessons in the game
    def getLessonList(self):
        return self.allLessons

    #returns the number of "words" in a particular lesson
    #returns -1 if there is no such lesson for key lessonName.
    def getLessonLength(self, lessonName):
        if self.lessonLengths.__contains__(lessonName):
            return self.lessonLengths[lessonName]
        return -1

    #returns the number of total lessons from a lesson plan
    def getNumLessons(self):
        return self.numLessons

    #Removes all whitespace surrounding toClear
    def clearWhiteSpace(self,toClear):
        while(toClear[0].isspace()):
            toClear = toClear[1:]
        while(toClear[-1].isspace()):
            toClear = toClear[0:-1]
        return toClear            

    #Items are CSVs on a single line.
    #Keep pulling off values until we end the line
    def parseLine(self, inputLine, wordList):
        nextComma = inputLine.find(",")
        while nextComma != -1:
            nextLesson = inputLine[0:nextComma]
            inputLine = inputLine[nextComma+1:]
            nextLesson = self.clearWhiteSpace(nextLesson)
            wordList.append(nextLesson)
            nextComma = inputLine.find(",")
        inputLine = self.clearWhiteSpace(inputLine)
        wordList.append(inputLine)

    #loads in data from a file for lesson plans
    #we assume the lesson plan file is a comma-seperated values file
    #the first line will be a list of all the lessons
    #the remaining lines, in order, will be the words in each lesson (one lesson per line)
    #
    #returns True if it successfully completes loading the lessons
    #and False if there is some error along the way
    def loadLessonPlan(self, lessonFileName):
        #attempt to load the lesson plans file
        try:
            lessonFile = file("%s" % lessonFileName)
        except IOError, error:
            print "unable to read file %s : %s" % (lessonFileName, error)
            return False

        # the file does exist, so we attempt to load the list of lessons
        # if the file is empty, we know we have a malformed file.
        lineInput = lessonFile.readline()
        lineInput = lineInput[0:lineInput.find("\n")]
        if lineInput == '':
            print "lesson file %s is empty.  Unable to load." % lessonFileName
            del lessonFile
            return False
        self.numLessons = lineInput.count(",")+1
        self.parseLine(lineInput, self.allLessons)        


        lessons = 0
        lineInput = lessonFile.readline()
        if lineInput.find("\n") != -1:
            lineInput = lineInput[0:lineInput.find("\n")]
        #load the lessons.  We want to make sure the number of lessons we load for
        #is the same as the number of lessons loaded.
        while lineInput != '':
            if lessons == self.numLessons:
                print "Too many lessons in file %s.  Unable to load." % lessonFileName
                del lessonFile
                return False
            self.lessonWords[self.allLessons[lessons]] = []
            self.lessonLengths[self.allLessons[lessons]] = lineInput.count(",")+1
            self.parseLine(lineInput, self.lessonWords[self.allLessons[lessons]])
            lessons = lessons + 1
            lineInput = lessonFile.readline()
            if lineInput.find("\n") != -1:
                lineInput = lineInput[0:lineInput.find("\n")]
        #make sure we loaded the correct number of lessons.
        if lessons < self.numLessons:
            print "Too few lessons listed in file %s.  Unable to successfully load." % lessonFileName
            del lessonFile
            return False

        #we succcessfully loaded
        del lessonFile
        return True